package com.erpanderp.wam.adm.model.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.CacheMode;
import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.adm.model.entity.Role;

import com.erpanderp.wam.adm.model.entity.Service;
import com.erpanderp.wam.adm.model.entity.ServiceGroup;

import com.erpanderp.wam.usm.model.entity.leave.LeaveType;
import com.erpanderp.wam.util.CommonLookup;

import com.erpanderp.wam.util.HibernateUtil;

public class RoleDAO {
	public Role createRole(Role role) {
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();
			session.setFlushMode(FlushMode.AUTO);
			session.save(role);
			session.getTransaction().commit();
		} catch (Exception e) {
			return null;
		}
		return role;
	}
	
	public Role updateRole(Role role) {
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();
			session.setFlushMode(FlushMode.AUTO);
			session.clear();
			session.saveOrUpdate(role);
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return role;
	}

	public boolean isRoleAvailable(String role) {
		boolean isRoleAvailable = false;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();
			String queryString = "from Role r where r.role = :role";
			Query query = session.createQuery(queryString).setString("role",
					role);
			List temp = query.list();
			if (temp == null || temp.size() == 0) {
				isRoleAvailable = true;
			}
			session.close();
			return isRoleAvailable;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean isRoleDisplayNameAvailable(String displayName) {
		boolean isRoleDisplayNameAvailable = false;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();
			String queryString = "from Role r where r.displayName = :displayName";
			Query query = session.createQuery(queryString).setString(
					"displayName", displayName);
			List temp = query.list();
			if (temp == null || temp.size() == 0) {
				isRoleDisplayNameAvailable = true;
			}
			session.close();
			return isRoleDisplayNameAvailable;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/*
	 * 
	 * Ram
	 */
	public List<Role> getAllRoles() {
		List<Role> allRoles = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			Query query = session.createQuery("from Role r");
			query.setCacheable(false);
			allRoles = (List<Role>) query.list();
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return allRoles;

	}

	public Role getRoleByRoleID(Long roleId) {
		Role role = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from Role r where roleId=:roleId";
			Query query = session.createQuery(queryString).setLong("roleId",
					roleId);
			query.setCacheable(false);
			role = (Role) query.uniqueResult();
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return role;
	}

	public List<Service> getServicesByRoleId(Long roleId) {
		List<Service> services = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "select rs.service from RoleService rs where rs.role.roleId=:roleId";
			Query query = session.createQuery(queryString).setLong("roleId",
					roleId);
			query.setCacheable(false);
			services = (List<Service>) query.list();
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return services;
	}

	public List<ServiceGroup> getServiceGroupsByRoleId(Long roleId) {
		List<ServiceGroup> serviceGroups = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "select rsg.serviceGroup from RoleServiceGroup rsg where rsg.role.roleId=:roleId";
			Query query = session.createQuery(queryString).setLong("roleId",
					roleId);
			query.setCacheable(false);
			serviceGroups = (List<ServiceGroup>) query.list();
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return serviceGroups;
	}
	
	public List<LeaveType> getLeaveTypesByRoleId(Long roleId) {
		List<LeaveType> leaveTypes = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "select lr.leaveType from LeaveTypeRole lr where lr.role.roleId=:roleId";
			Query query = session.createQuery(queryString).setLong("roleId",
					roleId);
			query.setCacheable(false);
			leaveTypes = (List<LeaveType>) query.list();
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return leaveTypes;
	}

	/*
	 * 
	 */

	public Role getAdministratorStandardRole() {
		Role administratorStandardRole;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();
			String queryString = "from Role r where r.role = :role";
			Query query = session.createQuery(queryString).setString("role",
					CommonLookup.STANDARD_ROLE_ADMINISTRATOR);
			List temp = query.list();
			administratorStandardRole = (Role) temp.get(0);
			session.close();
			return administratorStandardRole;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public Role getHRStandardRole() {
		Role hrStandardRole;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			String queryString = "from Role r where r.role = :role";
			Query query = session.createQuery(queryString).setString("role",
					CommonLookup.STANDARD_ROLE_HR);
			List temp = query.list();
			hrStandardRole = (Role) temp.get(0);
			session.close();
			return hrStandardRole;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String a[]) {
		Role role = new Role();
		System.out.println("ROLE " + new RoleDAO().createRole(role));
		System.out.println("ROLE " + role.getRoleId());
		System.out.println(new RoleDAO().getServiceGroupsByRoleId(1l));
	}

}
